import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy;
import org.apache.log4j.Logger;

public class SystemConfig {

    private static Logger logger = Logger.getLogger(SystemConfig.class);

    private static  PropertiesConfiguration config;

    static {
        try {
            //实例化一个PropertiesConfiguration
            config = new PropertiesConfiguration();
            //设置reload策略，这里用当文件被修改之后reload（默认5s中检测一次）
            config.load("E:\\MyUtil\\src\\main\\resources\\log4j.properties");
            FileChangedReloadingStrategy strategy = new FileChangedReloadingStrategy();
            strategy.setRefreshDelay(10);
            config.setReloadingStrategy(strategy);
        } catch (ConfigurationException e) {
            logger.error("init static block error. ", e);
        }
    }

    public static synchronized String getProperty(String key) {
        return  config.getProperty(key).toString();
    }

    public static void main(String[] args) throws InterruptedException {
        for (;;) {
            System.out.println(SystemConfig.getProperty("log4j.appender.file.MaxBackupIndex"));
            Thread.sleep(2000);
        }
    }
}
